#include "tools.h"
#include "getch.h"
#include "account.h"
#include "book.h"
#include "book_service_view.h"
#include "book_service.h"

//显示管理菜单
void BookServiceView::menu(void)
{
	while(1)
	{
		system("clear");
		cout << "＊＊＊欢迎使用指针图书馆系统＊＊＊" << endl;
		cout << "[1].借书" << endl;
		cout << "[2].还书" << endl;
		cout << "[3].查询用户信息" << endl;
		cout << "[4].查询图书" << endl;
		cout << "[5].增加图书" << endl;
		cout << "[6].删除图书" << endl;
		cout << "[7].删除书单" << endl;
		cout << "[8].查找书单" << endl;
		cout << "[9].修改图书信息" << endl;
		cout << "[0].充值余额"  <<endl;
		cout << "[q].退出" << endl;
		cout << "请选择:";
		char cmd = getch();
		cout << cmd << endl;
		switch(cmd)
		{
			case '1' : borrowBook();break;
			case '2' : returnBook();break;
			case '3' : queryUser();break;
			case '4' : queeryBook();break;
			case '5' : addBook();break;
			case '6' : delBook();break;
			case '7' : delBookList();get_char();break;
			case '8' : findBookList();get_char();break;
			case '9' : modifyBook();break;
			case '0' : recharge();break;
			case 'q' : return;
		}
	}
}

//借书：根据图书ID或书名从数据库中查找对应图书检测对应数量并出借。
void BookServiceView::borrowBook(void)
{
	system("clear");
	unsigned int id;
	unsigned int uid;
	cout << "请输入图书id:";
	cin >> id;
	cout << "请输入账户id:";
	cin >> uid;
	BookService bookservice;
	int cnt = bookservice.borrowBook(id,uid);
	
	if(0 == cnt)
	{
		cout << "借书成功!!!" << endl;
	}
	else if(-1 == cnt)
	{
		cout << "图书未上架!!!" << endl;
	}
	else if(-2 == cnt)
	{
		cout << "图书余量不足!!!" << endl;
	}
	else if(-3 == cnt)
	{
		cout << "余额不足，需要充值!!!" << endl;
	}
	else if(-4 == cnt)
	{
		cout << "该书不存在!!!" << endl;
	}
	else if(-5 == cnt)
	{
		cout << "用户id错误!!!" << endl;
	}
	get_char();
	return;
}

//还书：根据图书ID或书名从数据库中查找对应图书将数量加一，并设置归还
void BookServiceView::returnBook(void)
{
	system("clear");
	unsigned int id;
	unsigned int uid;
	cout << "请输入图书id:";
	cin >> id;
	cout << "请输入账户id:";
	cin >> uid;
	BookService bookservice;
	int flag = bookservice.returnBook(id,uid);
	if( 0 == flag)
	{
		cout << "还书成功!!!" << endl;
	}
	if( flag > 0 )
	{
	    cout << "还书成功，超过约定日期已扣费"<< flag << "元" << endl;
	}
	
	if( -1 == flag)
	{
	    cout << "图书id或用户id输入错误!" << endl;
	}
	if( -2 == flag)
	{
	    cout << "未借出此书!" << endl;
	}
	get_char();
	return;
}

/*查询用户信息：根据用户ID或用户名查询用户信息，其中包括用户昵称；注册时间；信用分（初始100，逾期归还将会减分）；用户借书记录：借书时间，书名、约定归还时间及实际归还时间*/
void BookServiceView::queryUser(void)
{
	system("clear");
	unsigned int uid;
	cout << "请输入账户id:";
	cin >> uid;
	BookService bookservice;
	Account* account = bookservice.queryUser(uid);
	if(account == NULL)
	{
		cout << "账户id错误" << endl;
		get_char();
		delete account;
		return;
	}
	cout << "用户名称:" << account->m_name << endl;
	cout << "用户id:" << account->m_id << endl;
	cout << "注册时间:" << 1900+account->m_regTime.tm_year << "-" << 1+account->m_regTime.tm_mon << "-" << 
		account->m_regTime.tm_mday << "-" << account->m_regTime.tm_hour << "-" << account->m_regTime.tm_min << "-" 
		<< account->m_regTime.tm_sec << endl;
	//cout << "用户信用值:" << account->m_credit << endl;
	cout << "用户密码:" << account->m_passwd << endl;
	cout << "剩余金额:" << account->m_money << endl;
	cout << "押金:" << account->m_deposit << endl;
	cout << "借书记录:" << endl;

	cout << "编号\t书号\t书名\t\t借书人ID\t借书人姓名\t租借天数\t借书日期\t\t是否归还\t归还日期" << endl;
	for(unsigned int i=0;i<account->m_record.size();i++)
	{
		cout << account->m_record.at(i).m_id << "\t" << account->m_record.at(i).m_bookId << "\t《" 
			 << account->m_record.at(i).m_bookName.c_str() << "》\t"<< account->m_record.at(i).m_perId << "\t\t"
			 << account->m_record.at(i).m_name.c_str() << "\t\t"<< account->m_record.at(i).m_day << "\t\t"
			 << 1900+account->m_record.at(i).m_broTime.tm_year << "-" << 1+account->m_record.at(i).m_broTime.tm_mon 
			 << "-" << account->m_record.at(i).m_broTime.tm_mday << "-" << account->m_record.at(i).m_broTime.tm_hour
			 << "-" << account->m_record.at(i).m_broTime.tm_min << "-" << account->m_record.at(i).m_broTime.tm_sec 
			 << "\t" << account->m_record.at(i).m_isReturn << "\t\t";
		if(account->m_record.at(i).m_isReturn)
		{
			cout << 1900+account->m_record.at(i).m_retTime.tm_year << "-" << 1+account->m_record.at(i).m_retTime.tm_mon
				 << "-" << account->m_record.at(i).m_retTime.tm_mday << "-" << account->m_record.at(i).m_retTime.tm_hour
				 << "-" << account->m_record.at(i).m_retTime.tm_min << "-" << account->m_record.at(i).m_retTime.tm_sec;
		}
		cout << endl;
	}
	get_char();
	delete account;
	return;
}

void BookServiceView::recharge(void)
{
	system("clear");
    unsigned int id = 0;
    cout << "请输入您要充值的账户id:";
    cin  >> id; getchar();
    while(1)
	{
	    cout << "[1]充值余额" << endl;
	    cout << "[2]充值押金" << endl;
	    cout << "[3]退出充值" << endl;
	    char cmd = getch();
	    cout << cmd << endl;
	    if('1' == cmd)
	    {
	        unsigned int money =0;
	        cout << "请输入您要充值的金额:" ;
	        cin >> money; getchar();
	        BookService m;
	        if( true == m.recharge_balance(id,money))
	        {
	            cout << "充值成功!" << endl;
	            get_char();
	            return;
	        }
	        else
	        {
	            cout << "账户id错误" << endl;
	            get_char();
	            return;
	        }
	    }
	    if('2' == cmd)
	    {
	        unsigned int money =0;
	        cout << "请输入您要充值的押金金额:" ;
	        cin >> money; getchar();
	        BookService m;
	        if( true == m.recharge_deposit(id,money))
	        {
	            cout << "充值成功!" << endl;
	            get_char();
	            return;
	        }
	        else
	        {
	            cout << "账户id错误" << endl;
				get_char();
	            return;
	        }
	    }
	    if('3' == cmd)
	    {
	        return;
	    }
	 }
}


void BookServiceView::_queerBook(Book* book)// 显示图书信息
{
	system("clear");
	cout << "书名:《" << book->m_name << "》" << endl;
	cout << "作者:" << book->m_author << endl;
	cout << "书号:" << book->m_id << endl;
	cout << "类别:" << book->m_type << endl;
	cout << "入库时间:";
	cout << 1900+book->m_intime.tm_year << ":" << 1+book->m_intime.tm_mon << ":" << book->m_intime.tm_mday 
			<< ":" << book->m_intime.tm_hour << ":" << book->m_intime.tm_min << ":" << book->m_intime.tm_sec << endl;
					 
	cout << "剩余数量:" << book->m_amout << endl;
	cout << "总数:" << book->m_total << endl;
	cout << "评分:";
	int score = book->m_score*10;
	switch(score)
	{
		case  0 ... 15 : cout << "★☆☆☆☆" << endl; break;
		case 16 ... 25 : cout << "★★☆☆☆" << endl; break;
		case 26 ... 35 : cout << "★★★☆☆" << endl; break;
		case 36 ... 45 : cout << "★★★★☆" << endl; break;
		case 46 ... 50 : cout << "★★★★★" << endl; break;
	}
	cout << "评分人数:" << book->m_sco_num << endl;
	cout << "推荐数:" << book->m_recom << endl;
	cout << "上架状态:" << book->m_isShelf << endl;
	cout << "简介:" << book->m_syn << endl;
	cout << "借书记录数:" << book->m_numRecord << endl;
	if(book->m_numRecord)
	{
		cout << "编号\t书号\t 书名\t\t借书人ID\t借书人姓名\t租借天数\t借书日期\t\t是否归还\t归还日期" << endl;
		//vector<Record>::iterator it;
		for( vector<Record>::iterator it = book->m_record.begin(); it != book->m_record.end(); it++ )
		{
			Record record = *it;
			cout << record.m_id << "\t" << record.m_bookId << "\t《" << record.m_bookName << "》\t" << record.m_perId 
					<< "\t\t" << record.m_name << "\t\t" << record.m_day << "\t\t" << 1900+record.m_broTime.tm_year << ":" 
					<< 1+record.m_broTime.tm_mon << ":" << record.m_broTime.tm_mday << ":" << record.m_broTime.tm_hour 
					<< ":" << record.m_broTime.tm_min << ":" << record.m_broTime.tm_sec << "\t" << record.m_isReturn 
					<< "\t\t";
			if(record.m_isReturn)
			{
				cout << 1900+record.m_retTime.tm_year << ":" << 1+record.m_retTime.tm_mon 
					 << ":" << record.m_retTime.tm_mday << ":" << record.m_retTime.tm_hour
					 << ":" << record.m_retTime.tm_min << ":" << record.m_retTime.tm_sec;
			}
			cout << endl;
		}
	}
	cout << "评论数:" << book->m_vecCom.size() << endl;

    //vector<Comment> m_vecCom; //评论
    for(unsigned int i=0; i<book->m_vecCom.size();i++)
    {
        cout << "——————————————————————————————" << endl;
        cout << i+1 << " 昵称：" << book->m_vecCom.at(i).m_name << " " << 1900+book->m_vecCom.at(i).m_time.tm_year << "/"
                << 1+book->m_vecCom.at(i).m_time.tm_mon << "/" << book->m_vecCom.at(i).m_time.tm_mday << " "
                << book->m_vecCom.at(i).m_time.tm_hour << ":" << book->m_vecCom.at(i).m_time.tm_min << ":"
                << book->m_vecCom.at(i).m_time.tm_sec << " 评分:";
                score = book->m_vecCom.at(i).m_score*10;
                switch(score)
                {
                    case  0 ... 15 : cout << "★☆☆☆☆" << endl; break;
                    case 16 ... 25 : cout << "★★☆☆☆" << endl; break;
                    case 26 ... 35 : cout << "★★★☆☆" << endl; break;
                    case 36 ... 45 : cout << "★★★★☆" << endl; break;
                    case 46 ... 50 : cout << "★★★★★" << endl; break;
                }
        cout << "内容:" << book->m_vecCom.at(i).m_content << endl;
    }
	//get_char();
}

void _BookList(BookList* booklist)
{
	cout << "书单id:" << booklist->m_id << endl;
	cout << "书单名称:" << booklist->m_name << endl;
	cout << "书名\t作者\t书号\t类别" << endl;
	map<unsigned int , BookInfo>::iterator it; 
	for( it = booklist->mapBookInfo.begin(); it != booklist->mapBookInfo.end(); it++ )
	{
		BookInfo bookinfo = it->second;
		cout << bookinfo.m_name << "\t" << bookinfo.m_author << "\t" << bookinfo.m_id << "\t" << bookinfo.m_type << endl;
	}
}

//查询图书：根据图书ID或图书名查询对应图书信息。
void BookServiceView::queeryBook(void)
{
	system("clear");
	cout << "1.通过图书id查询" << endl;
	cout << "2.通过图书名查询" << endl;
	cout << "请选择图书查询方式:";
	char cmd = getch();
	cout << cmd << endl;
	if('1' == cmd)
	{
		unsigned int id;
		cout << "请输入图书id:";
		cin >> id;
		BookService bookservice;
		Book* book = bookservice.queeryBook(id);
		if(book == NULL)
		{
			cout << "图书id错误" << endl;
			get_char();
			delete book;
			return;
		}
		_queerBook(book);
		get_char();
		delete book;
		return;
	}
	else if('2' == cmd)
	{
		string bookName;
		cout << "请输入图书名:";
		cin >> bookName;
		BookService bookservice;
		vector<BookInfo> bookinfo;
		bookinfo = bookservice.queeryBook(bookName.c_str());
		if(0 == bookinfo.size())
		{
			cout << "没有这个名字的书" << endl;
			get_char();
			return;
		}
		int num = 0;
		for(vector<BookInfo>::iterator it = bookinfo.begin();it != bookinfo.end(); it++)
		{
			BookInfo book = *it;
			cout << "第[" << ++num << "]本" << endl; 
			cout << "书名：" << book.m_name << endl;
			cout << "作者：" << book.m_author << endl;
			cout << "书号：" << book.m_id << endl;
			cout << "类别：" << book.m_type << endl;
		}
		get_char();
		return;
	}
}

char *str_get(char* str,int size)
{
	fgets(str,size,stdin);
	for(int i=0;i<size;i++)
	{
		if(str[i] == '\n')
		{
			str[i] = 0;
			return str;
		}
	}
	return str;
}

//增加图书：新增图书，自动为图书生成ID。
void BookServiceView::addBook(void)
{
	system("clear");
	cout << "请输入书名:";
	char name1[50];
	char* name=str_get(name1,sizeof(name1));
	
	string author;
	cout << "请输入作者:";
	cin >> author;
	
	unsigned int id;
	id = generator_BookId();
	
	string type;
	cout << "请输入类别:";
	cin >> type;
	
	time_t timep;
	struct tm *p;
	time(&timep);
	p=localtime(&timep);
	
	unsigned int total;
	cout << "请输入书籍总数:";
	cin >> total;

	cout << "请输入书籍简介:";
	char syn1[2048];
	char* syn=str_get(syn1,sizeof(syn1));
	
	bool isShelf;
	cout << "是否上架（1上架/0不上架）:";
	cin >> isShelf;
	double d = 0;
	unsigned int ui = 0;
	Book book(name,author.c_str(),id,type.c_str(),*p,total,total,d,ui,ui,syn,isShelf,ui,ui);
	BookService bookservice;
	if(bookservice.addBook(book))
	{
		cout << "添加成功" << endl;
		get_char();
		return;
	}
	cout << "添加失败" << endl;
	get_char();
	return;
}

//删除图书：删除图书，只将数量设置为0仍然保留图书其他信息。
void BookServiceView::delBook(void)
{
	system("clear");
	unsigned int id;
	cout << "请输入图书id:";
	cin >> id;
	BookService bookservice;
	if(bookservice.delBook(id))
	{
		cout << "删除图书成功" << endl;
	}
	else
	{
		cout << "删除图书失败" << endl;
	}
	get_char();
	return;
}

//删除书单：输入一个书单名或书单ID，显示书单信息，并确认是否删除
void BookServiceView::delBookList(void)
{
	system("clear");
	unsigned int id;
	cout << "请输入书单id:";
	cin >> id;
	BookService bookservice;
	BookList* booklist = bookservice.findBookList(id);
	if(booklist == NULL)
	{
		cout << "书单id错误" << endl;
		delete booklist;
		return;
	}
	_BookList(booklist);
	
	char cmd;
	cout << "请选择是否删除(y/n)" << endl;
	cin >> cmd;
	if('y' == cmd)
	{
		if(bookservice.delBookList(id))
		{
			cout << "书单删除成功" << endl;
			return;
		}
	}
	else if('n' == cmd)
	{
		cout << "书单删除失败" << endl;
		return;
	}
	cout << "输入错误！！" << endl;
	delete booklist;
	return;
}

//查找书单：输入一个书单名或书单ID，查找书单并显示
void BookServiceView::findBookList(void)
{
	system("clear");
	cout << "****书单列表****" << endl;
	cout << "书单号\t\t\t书单名" << endl;
	DIR* dir = opendir("../data/booklist");
	for(struct dirent* d = readdir(dir);NULL != d;d= readdir(dir))
	{
		if(d->d_name[0] != '.')
		{
			int num =0;
			sscanf(d->d_name,"%d.txt",&num);
			BookService bookservice;
			BookList* booklist = bookservice.findBookList(num);
			cout << num << "\t\t\t" << booklist->m_name << endl;
			delete booklist;
		}
	}
	unsigned int id;
	cout << "请输入书单id:";
	cin >> id;
	BookService bookservice;
	BookList* booklist = bookservice.findBookList(id);
	if(booklist == NULL)
	{
		cout << "书单id错误" << endl;
		delete booklist;
		return;
	}
	_BookList(booklist);
	delete booklist;
	return;
}

//修改图书信息：输入图书ID或书名，修改图书数量，删除图书评论，修改其他信息等
void BookServiceView::modifyBook(void)
{
	system("clear");
	unsigned int id = 0;
	cout << "请输入您要修改的图书ID：" ;
	cin >> id; getchar();
	BookServiceData m;
	Book* book = m.load(id);
	if(NULL == book) 
	{
	    cout << "ID号错误" << endl;
	    get_char();
	    return ;
	}
	while(1)
	{
		_queerBook1(book);
	    char cmd = getch();
	    cout << cmd << endl;
	    if('1' == cmd)
	    {   
	        unsigned int num = 0;
	        cout << "请输入您要删除的评论编号:";
	        cin >> num ; getchar();
            vector<Comment>::iterator it;
            for(it = book->m_vecCom.begin();it != book->m_vecCom.end();it++)
            {
                if(num == it->m_id)
                {
                    book->m_vecCom.erase(it);
                }    
            }
	    }
	    if('2' == cmd)
	    {
	        cout << "请输入您要修改的书名:" << endl;
	        char name1[50];
			char* name2=str_get(name1,sizeof(name1));
			string name = name2;
	        book->m_name = name;
	    }
	    if('3' == cmd)
	    {
	        string name;
	        cout << "请输入您要修改的作者名:" << endl;
	        cin >> name;getchar();
	        book->m_author = name;
	    }
	    if('4' == cmd)
	    {
	        string type;
	        cout << "请输入您要修改的书的类别:" << endl;
	        cin >> type;getchar();
	        book-> m_type = type;
	    }
	    if('5' == cmd)
	    {
	        unsigned int amout;
	        cout << "请输入您要修改的书的余量:" << endl;
	        cin >> amout;getchar();
	        book-> m_amout = amout;
	    }
	    if('6' == cmd)
	    {
	        unsigned int total;
	        cout << "请输入您要修改的书的总量:" << endl;
	        cin >> total;getchar();
	        book-> m_total = total;
	    }
	    if('7' == cmd)
	    {
	        cout << "请输入您要修改的图书简介:" <<endl;
	        char syn1[50];
			char* syn2=str_get(syn1,sizeof(syn1));
			string syn = syn2;
	        book->m_syn = syn;
	    }
	    if('8' == cmd)
	    {
	        bool isShelf;
	        cout << "请输入您的上下架信息:" << endl;
	        cin >> isShelf;getchar();
	        book->m_isShelf = isShelf;
	    }
	    if('q' == cmd)
	    {
	        delete book;
	        return;
	    }
	    BookServiceData m;
	    m.save(*book);
	}  
}



void BookServiceView::_queerBook1(Book* book)// 显示图书信息
{
	system("clear");
    cout << "书名:" << book->m_name;		printf("\033[%d;%dH|菜单\n", (1), (45));
    cout << "作者:" << book->m_author;	printf("\033[%d;%dH|[1]删除评论 [2]修改书名\n", (2), (45));
    cout << "书号:" << book->m_id;		printf("\033[%d;%dH|[3]修改作者 [4]修改类别\n", (3), (45));
    cout << "类别:" << book->m_type;		printf("\033[%d;%dH|[5]修改余量 [6]修改总量\n", (4), (45));
	cout << "入库时间:" << 1900+book->m_intime.tm_year << ":" << 1+book->m_intime.tm_mon << ":" << book->m_intime.tm_mday+1 
			<< ":" << book->m_intime.tm_hour << ":" << book->m_intime.tm_min << ":" << book->m_intime.tm_sec;
            printf("\033[%d;%dH|[7]修改简介 [8]上架下架\n", (5), (45));
    cout << "剩余数量:" << book->m_amout;printf("\033[%d;%dH|[q]退出修改\n", (6), (45));
	cout << "总数:" << book->m_total << endl;
	cout << "评分:";
	int score = book->m_score*10;
	switch(score)
	{
		case  0 ... 15 : cout << "★☆☆☆☆" << endl; break;
		case 16 ... 25 : cout << "★★☆☆☆" << endl; break;
		case 26 ... 35 : cout << "★★★☆☆" << endl; break;
		case 36 ... 45 : cout << "★★★★☆" << endl; break;
		case 46 ... 50 : cout << "★★★★★" << endl; break;
	}
	cout << "评分人数:" << book->m_sco_num << endl;
	cout << "推荐数:" << book->m_recom << endl;
	cout << "上架状态:" << book->m_isShelf << endl;
	cout << "简介:" << book->m_syn << endl;
	cout << "借书记录数:" << book->m_numRecord << endl;
	if(book->m_numRecord)
	{
		cout << "编号\t书号\t 书名\t\t借书人ID\t借书人姓名\t租借天数\t借书日期\t\t是否归还\t归还日期" << endl;
		//vector<Record>::iterator it;
		for( vector<Record>::iterator it = book->m_record.begin(); it != book->m_record.end(); it++ )
		{
			Record record = *it;
			cout << record.m_id << "\t" << record.m_bookId << "\t《" << record.m_bookName << "》\t" << record.m_perId 
                    << "\t\t" << record.m_name << "\t\t" << record.m_day << "\t\t" << 1900+record.m_broTime.tm_year << "-"
                    << 1+record.m_broTime.tm_mon << "-" << record.m_broTime.tm_mday << " " << record.m_broTime.tm_hour
					<< ":" << record.m_broTime.tm_min << ":" << record.m_broTime.tm_sec << "\t" << record.m_isReturn 
					<< "\t\t";
			if(record.m_isReturn)
			{
				cout << 1900+record.m_retTime.tm_year << ":" << 1+record.m_retTime.tm_mon 
					 << ":" << record.m_retTime.tm_mday << ":" << record.m_retTime.tm_hour
					 << ":" << record.m_retTime.tm_min << ":" << record.m_retTime.tm_sec;
			}
			cout << endl;
		}
	}
	cout << "评论数:" << book->m_vecCom.size() << endl;
	
    //vector<Comment> m_vecCom; //评论
        for(unsigned int i=0; i<book->m_vecCom.size();i++)
        {
            cout << "——————————————————————————————" << endl;
            cout << i+1 << " 昵称：" << book->m_vecCom.at(i).m_name << " " << 1900+book->m_vecCom.at(i).m_time.tm_year << "/"
                    << 1+book->m_vecCom.at(i).m_time.tm_mon << "/" << book->m_vecCom.at(i).m_time.tm_mday << " "
                    << book->m_vecCom.at(i).m_time.tm_hour << ":" << book->m_vecCom.at(i).m_time.tm_min << ":"
                    << book->m_vecCom.at(i).m_time.tm_sec << " 评分:";
                    score = book->m_vecCom.at(i).m_score*10;
                    switch(score)
                    {
                        case  0 ... 15 : cout << "★☆☆☆☆" << endl; break;
                        case 16 ... 25 : cout << "★★☆☆☆" << endl; break;
                        case 26 ... 35 : cout << "★★★☆☆" << endl; break;
                        case 36 ... 45 : cout << "★★★★☆" << endl; break;
                        case 46 ... 50 : cout << "★★★★★" << endl; break;
                    }
            cout << "内容:" << book->m_vecCom.at(i).m_content << endl;
        }
	cout << "请输入:";
}

